
import tkinter
from PIL import Image
from tkinter import filedialog
from tkinter import messagebox
from PIL import Image
import re

from rembg import remove

# rembg得在python3.8以上版本，64位的才行，我用python3.7 32位的报错，换到python3.10 64位就好了
# 如果要抠图的话，打包后得上百M，不抠图就会很小，不扣图的话就把remove相关的代码去掉

win = tkinter.Tk()
win.title('icon制作工具        Author : Shi Sixian')
win.geometry('600x400')
win.option_add('*font', '黑体 12')


def chose_image():
    image_path = tkinter.filedialog.askopenfilename()
    if image_path != '':
        image_path_entry.delete(0, 'end')
        image_path_entry.insert(0, image_path)
    # else:
    #     print('no chose')


def chose_ico_path():
    ico_path = tkinter.filedialog.askdirectory()
    if ico_path != '':
        ico_path_entry.delete(0, 'end')
        ico_path_entry.insert(0, ico_path)


def create_ico():
    try:
        image_path = image_path_entry.get()
        ico_path = ico_path_entry.get()
        ico_name = ico_name_entry.get()
        ico_size_x = ico_size_x_entry.get()
        ico_size_y = ico_size_y_entry.get()
        if len(image_path) > 1 and len(ico_path) > 1 and len(ico_name) > 1 and ico_size_x.isdigit() and ico_size_y.isdigit():
            matting = matting_value.get()
            # print(matting)
            if matting:
                try:
                    pattern = '(.+)\.(.+)'
                    image_path2 = re.findall(pattern, image_path)
                    type = image_path2[0][1]
                    new_name = image_path2[0][0]
                    new_path = str(new_name) + '-1.' + str(type)
                    # print(new_path)
                    # 这里在原来图片的路径，再生成1张扣过图的图片，图片名后面加上-1

                    # 电脑第一次执行remove时，会从GitHub上下载个模块，放到如下路径。大小为167M。因为是国外网站，下载很慢，也可以直接把模块放到对应路径。
                    # C:\Users\ocu00394\.u2net\u2net.onnx
                    with open(image_path, 'rb') as i:
                        with open(new_path, 'wb') as o:
                            input = i.read()
                            output = remove(input)
                            o.write(output)
                    image_path = new_path
                except Exception as ex2:
                    error = str(ex2)
                    messagebox.showwarning(title='警告', message=f'扣图失败，此次未执行抠图，{error}')
            img = Image.open(image_path)
            ico_path = ico_path + '\\' + ico_name + '.ico'
            icon_sizes = [(int(ico_size_x), int(ico_size_y))]
            img.save(ico_path, sizes=icon_sizes)
            messagebox.showinfo(title='生成结果', message='icon已制作成功')
        else:
            messagebox.showerror(title='报错', message='条件没输全，请检查输入值')
    except Exception as ex:
        error = str(ex)
        messagebox.showerror(title='报错', message=error)


image_path_button = tkinter.Button(win, text='选择图片', bg='skyblue', command=chose_image)
image_path_button.place(x=20, y=30)
image_path_entry = tkinter.Entry(win, width=68)
image_path_entry.place(x=20, y=60)

matting_label = tkinter.Label(win, text='选择是否抠图，默认不抠图，                         \n如果选择抠图，会在图片的路径下再生成1张抠图的图片。')
matting_label.place(x=20, y=100)
matting_value = tkinter.IntVar()
matting_radio1 = tkinter.Radiobutton(win, text='不扣图', value=0, variable=matting_value)
matting_radio1.place(x=20, y=140)
matting_radio2 = tkinter.Radiobutton(win, text='要抠图', value=1, variable=matting_value)
matting_radio2.place(x=120, y=140)
matting_value.set(0)

ico_path_button = tkinter.Button(win, text='生成ico的存放路径', bg='skyblue', command=chose_ico_path)
ico_path_button.place(x=20, y=190)
ico_path_entry = tkinter.Entry(win, width=68)
ico_path_entry.place(x=20, y=220)
ico_name_label = tkinter.Label(win, text='生成icon的名字：')
ico_name_label.place(x=20, y=250)
ico_name_entry = tkinter.Entry(win, width=30)
ico_name_entry.place(x=160, y=250)
ico_name_entry.insert(0, '图标')
ico_size_label = tkinter.Label(win, text='生成的ico尺寸：')
ico_size_label.place(x=20, y=280)
ico_size_x_entry = tkinter.Entry(win, width=10)
ico_size_x_entry.place(x=160, y=280)
ico_size_label2 = tkinter.Label(win, text='X')
ico_size_label2.place(x=240, y=280)
ico_size_y_entry = tkinter.Entry(win, width=10)
ico_size_y_entry.place(x=255, y=280)
ico_size_x_entry.insert(0, '64')
ico_size_y_entry.insert(0, '64')

create_button = tkinter.Button(win, text='生成icon', bg='skyblue', font='黑体 20', command=create_ico)
create_button.place(x=20, y=330)

win.mainloop()
